package com.jy.adscm.operation.purchase.controller;


import com.jy.adscm.operation.purchase.bean.Bom;
import com.jy.adscm.operation.purchase.bean.QueryByPageParameter;
import com.jy.adscm.operation.purchase.service.BomService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * bom清单表 前端控制器
 * </p>
 *
 * @author lzf
 * @since 2019-05-05
 */
@RestController
@RequestMapping("adscm/operation/bom")
public class BomController {
    @Autowired
    BomService bomService;
    @Autowired
    private RedisTemplate<Object, Token> tokenRedisTemplate;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody Bom bom, HttpServletRequest request){
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        bom.setSignedPerson(token.getUser().getName());
        bom.setSignedTime(new Date());
        return new SimpleCoreResponse("200","新增成功",bomService.insert(bom));
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody Bom bom){
        return new SimpleCoreResponse("200","修改成功",bomService.updateById(bom));
    }
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable("id") Long id){
        return new SimpleCoreResponse("200","删除成功",bomService.deleteById(id));

    }


    @RequestMapping("queryByPage")
    public SimpleCoreResponse queryByPage(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                          @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "sortBy", required = false) String sortBy,
                                          @RequestParam(value = "desc",required = false)boolean desc,
                                          @RequestParam(value = "key", required = false) String key
                                          ){
        QueryByPageParameter queryByPageParameter=new QueryByPageParameter(currentPage,pageSize,sortBy,desc,key);
        return new SimpleCoreResponse("200","查询成功",bomService.queryByPage(queryByPageParameter));
    }

}

